package com.onpositive.wiki3.db.catmodel.matches;

import com.onpositive.wiki3.db.catmodel.Category;
import com.onpositive.wiki3.db.catmodel.ICategoryMatcher;
import com.onpositive.wiki3.db.impl.CountingVocabalary;

public final class YearCategoryMatcher implements ICategoryMatcher {
	public CountingVocabalary vs = new CountingVocabalary();

	public YearCategoryMatcher() {		
	}

	@Override
	public int accept(Category st) {
		String title=st.toString();
		int indexOf = title.indexOf('_');
		if (indexOf != -1) {
			try {
				if (!st.hasChildren()){
					return -1;
				}
				String substring = title.substring(0, indexOf);
				if (substring.endsWith("s")) {
					substring = substring.substring(0, substring.length() - 1);
				}
				// if (!st.hasChildren()){
				// return -1;
				// }
				Integer.parseInt(substring);
				String trim = title.substring(indexOf + 1).trim();
				//int[] parents = cm.getParents(st, 2);

				vs.add(trim);
				boolean isOnlyBranch=false;
				for (Category z:st.getChildCategories()){
					if (z.parentCount()==1){
						isOnlyBranch=true;	
					}					
				}
				if (isOnlyBranch){
					st.removeCategoryWithProxy();
				}
				else{
					st.remove();
					
				}				
			} catch (NumberFormatException e) {
				// TODO: handle exception
			}
		}
		return -1;
	}
}